﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.ASMS.Client.UI.Properties;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Pickers
{
    public partial class PayItemPicker : UserControl, IView
    {
        //当前页  
        private int _pageIndex = 0;
        //总共几页
        private int _pageCount = 0;
        Promise _promise = new Promise();

        public PayItemPicker()
        {
            InitializeComponent();
            this.GCData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("ok", Resources.isOK, "确定", OnOKActionClick))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", OnRefreshActionClick))
              .AddAction(new ActionNode("cancel", Resources.isDelete, "取消", OnCancelActionClick));
        }
        public void Active(ViewActiveArgs args)
        {
            _promise
                .Then(BindData)
                .Run(null);
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("选择其他收费/优惠");
        }
        #endregion

        #region Method
        /// <summary>
        /// 获取每页条数限制
        /// </summary>
        /// <returns></returns>
        private int GetPageLimit()
        {
            return 25;
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void BindData()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.EPayItem> result = new Service.QueryService().QueryPayItemPagedList(
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    Domain.Constant.CommonStatus.Enabled,
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action<PagedResult<WebRef.EPayItem>>((p) =>
                {
                    this.HideMask();
                    if (p.IsSuccess)
                    {
                        this.GCData.Paginate<WebRef.EPayItem>(p, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError("查询失败！");
                        _promise.Reject();
                    }
                }), result);
            });
        }
        #endregion

        #region actions
        private void OnOKActionClick(object obj1, object obj2)
        {
            int[] selRows = this.GVData.GetSelectedRows();
            if (selRows == null || selRows.Length == 0)
            {
                return;
            }
            WebRef.EPayItem supplier = GVData.GetRow(selRows[0]) as WebRef.EPayItem;
            UIContext.Current.ViewManager.CloseView(true, supplier);
        }

        private void OnCancelActionClick(object obj1, object obj2)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        private void OnRefreshActionClick(object obj1, object obj2)
        {
            _pageIndex = 0;
            BindData();
        }
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            BindData();
        }
        #endregion

        /// <summary>
        /// 双击行选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GVData_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = GVData.CalcHitInfo(new Point(e.X, e.Y));
                if (hInfo.InRow)
                {
                    if (GVData.FocusedRowHandle > -1)
                    {
                        WebRef.EPayItem supplier = GVData.GetFocusedRow() as WebRef.EPayItem;
                        UIContext.Current.ViewManager.CloseView(true, supplier);
                    }
                }
            }
        }
    }
}
